package com.example.demo.sqlplan.dto;

import cn.hutool.json.JSONUtil;
import com.example.demo.jsqlparser.SqlParserTool;
import com.example.demo.jsqlparser.SqlTypeEum;
import com.example.demo.test2.entity.Test2;
import com.example.demo.utils.JsonUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.Select;
import org.apache.poi.ss.formula.functions.T;

import java.io.StringReader;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname SqlAnalyzeTest
 * @description
 * @date 2022/08/15 9:26
 * <p>
 * ******************************************************
 */
public class SqlAnalyzeTest {

    public static void main(String[] args) throws JsonProcessingException, JSQLParserException {


//        String testSql = "  dsa dsa asdf fsad  ";
//        System.out.println("[" + testSql.trim() + "]");
//        String json = "{\n  \"query_block\": {\n    \"select_id\": 1,\n    \"r_loops\": 1,\n    \"r_total_time_ms\": 6358,\n    \"table\": {\n      \"table_name\": \"h\",\n      \"access_type\": \"ALL\",\n      \"possible_keys\": [\"PRIMARY\"],\n      \"r_loops\": 1,\n      \"rows\": 6,\n      \"r_rows\": 6,\n      \"r_total_time_ms\": 0.0635,\n      \"filtered\": 100,\n      \"r_filtered\": 100\n    },\n    \"block-nl-join\": {\n      \"table\": {\n        \"table_name\": \"wh\",\n        \"access_type\": \"hash_ALL\",\n        \"possible_keys\": [\"PRIMARY\"],\n        \"key\": \"#hash#PRIMARY\",\n        \"key_length\": \"4\",\n        \"used_key_parts\": [\"id\"],\n        \"ref\": [\"test.h.id\"],\n        \"r_loops\": 1,\n        \"rows\": 6,\n        \"r_rows\": 6,\n        \"r_total_time_ms\": 0.0285,\n        \"filtered\": 16.667,\n        \"r_filtered\": 100\n      },\n      \"buffer_type\": \"flat\",\n      \"buffer_size\": \"2Kb\",\n      \"join_type\": \"BNLH\",\n      \"r_filtered\": 100\n    }\n  }\n}";
//        ObjectMapper mapper = new ObjectMapper();
//        SqlPlanQueryBlockDTO value = mapper.readValue(json, SqlPlanQueryBlockDTO.class);
//        System.out.println(mapper.writeValueAsString(value));
//        String test1 = "{\n" +
//                "  \"id\": 0,\n" +
//                "  \"zi\": \"123\",\n" +
//                "  \"zi\": \"456\"\n" +
//                "}";
//        String test2 = "{\n" +
//                "  \"id\": 0,\n" +
//                "  \"zi\": \"456\",\n" +
//                "  \"zi\": \"123\"\n" +
//                "}";
////        JsonUtils.readValues()
//        Test2 test1Class = mapper.readValue(test1, Test2.class);
//        Test2 test2Class = mapper.readValue(test2, Test2.class);
//        System.out.println(mapper.writeValueAsString(test1Class));
//        System.out.println(mapper.writeValueAsString(test2Class));
//
//
//
//        String sql_text = "select * from test.`bmsql_warehouse` w where w.w_city in(\n" +
//                "    SELECT citysum2.city FROM (\n" +
//                "        SELECT * FROM (\n" +
//                "            SELECT h.h_city as city, COUNT(*) AS citysum FROM bmsql_house h GROUP BY h.h_city) AS citysum1\n" +
//                "        WHERE citysum1.citysum > 1) AS citysum2 order by citysum2.citysum asc\n" +
//                "    ) and w.id in (select s.id from bmsql_wh wh left join system_index s on wh.id = s.id) order by w.id asc limit 2;";
//        System.out.println(isSelect(sql_text));
//        Statement sqlStmt = CCJSqlParserUtil.parse(new StringReader(sql_text));
//        List<String> tableList = SqlParserTool.getTableList((Select) sqlStmt);
////        Map<String, String> alias = SqlParserTool.getParseAlias(sql_text);
//        System.out.println(tableList);
////        System.out.println(alias);


        String sql = "select\n" +
                "\tcurrent_schema as schema_name,\n" +
                "\tSQL_TEXT as sql_text,\n" +
                "\tDIGEST_TEXT as digest_text,\n" +
                "\tDIGEST as digest,\n" +
                "\tROUND(timer_wait * 10E-10, 3) as wait_time,\n" +
                "\tROWS_SENT as rows_sent,\n" +
                "\tNO_INDEX_USED as no_index_used,\n" +
                "\tDATE_SUB(NOW(),\n" +
                "\tinterval (\n" +
                "\tselect\n" +
                "\t\tVARIABLE_VALUE\n" +
                "\tfrom\n" +
                "\t\tinformation_schema.global_status\n" +
                "\twhere\n" +
                "\t\tVARIABLE_NAME = 'UPTIME') - TIMER_START * 10e-13 second) as start_time,\n" +
                "\tDATE_SUB(NOW(),\n" +
                "\tinterval (\n" +
                "\tselect\n" +
                "\t\tVARIABLE_VALUE\n" +
                "\tfrom\n" +
                "\t\tinformation_schema.global_status\n" +
                "\twhere\n" +
                "\t\tVARIABLE_NAME = 'UPTIME') - TIMER_END * 10e-13 second) as end_time,\n" +
                "\tCREATED_TMP_DISK_TABLES as created_tmp_disk_tables,\n" +
                "\tCREATED_TMP_TABLES as created_tmp_tables\n" +
                "from\n" +
                "\tperformance_schema.events_statements_history_long\n" +
                "having\n" +
                "\tstart_time > date_sub(now(),\n" +
                "\tinterval 10 minute)";
        sql = sql.replaceAll("\r|\n|\t", " ");
        boolean select = isSelect(sql);
        System.out.println(select);
    }

    private static boolean isSelect(String sqlText) throws JSQLParserException {
        SqlTypeEum sqlTypeEum = SqlParserTool.getSqlTypeEum(sqlText);
        return sqlTypeEum.equals(SqlTypeEum.SELECT) ? true : false;
    }
}
